﻿using Marbles.CMS.Core;
using Marbles.CMS.ContentTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace Marbles.CMS.Controls
{
    public partial class LoginControl : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.User.Identity.IsAuthenticated)
            {
                PageRendererBase pg = this.Page as PageRendererBase;
                Button btnAdmin = pg.FindControl<Button>("btnAdmin", LoginViewMain);
                btnAdmin.Visible = false;

                if (Roles.IsUserInRole("Administrator"))
                {                    
                    btnAdmin.Visible = true;
                }
            }
        }

        protected void LoginViewMain_LoggedIn(object sender, EventArgs e)
        {
            Login LoginControlMain = sender as Login;
            SiteController.Login(LoginControlMain.UserName, Page.Session.Timeout);
        }

        protected void LoginViewMain_ViewChanged(object sender, EventArgs e)
        {

        }
        protected void LoginViewMain_Logout(object sender, EventArgs e)
        {
            SiteController.Logout();
            Response.Redirect(Request.Url.ToString());
        }

        protected void btnAdmin_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Administrator/", true);
        }
    }
}